<?php

namespace app\model;

use think\Model;

class Disinfect extends Model
{
    const IS_DELETE_NO = 0;

    /**
     * @Description
     * @author: zj
     * @param int $factoryId
     * @param int $outId
     * @time 2024/03/12 16:12:18
     * @return mixed|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkOutIdIsExist(int $factoryId, int $outId)
    {
        $info = $this->field('id')
            ->where('factory_id', $factoryId)
            ->where('out_id', $outId)
            ->where('delete_time', self::IS_DELETE_NO)
            ->find();
        if (!$info) {
            return null;
        }

        return $info['id'];
    }

    /**
     * @Description 获取最新消毒时间
     * @author: zj
     * @param int $factoryId
     * @param int $outBarnId
     * @param int $outFenceId
     * @time 2024/03/19 16:41:36
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getLastDisinfectTime(int $factoryId,int $outBarnId = 0,int $outFenceId = 0)
    {
        $query = $this->alias('d')
            ->field('d.item_name,d.remark,d.record_time')
            ->where('d.factory_id',$factoryId)
            ->where('d.delete_time',self::IS_DELETE_NO);
        if ($outBarnId) {
            $query->join('fence f','f.out_barn_id = d.fence_id','left')
                ->where('f.out_barn_id',$outBarnId);
        }
        if ($outFenceId) {
            $query->where('d.fence_id',$outFenceId);
        }
        $info = $query->order('d.record_time DESC')
            ->find();

        if (!$info) {
            return [];
        }

        return $info->toArray();
    }

}